package cn.cs.controller;


import cn.cs.common.*;
import cn.cs.pojo.AppOrder;
import cn.cs.pojo.ClientUser;
import cn.cs.pojo.UserAddress;
import cn.cs.pojo.UserRepairApply;
import cn.cs.service.ClientUserService;
import cn.cs.service.UserAddressService;
import cn.cs.service.UserRepairApplyService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/userRepairApply")
public class UserRepairApplyController {

    @Resource
    private UserRepairApplyService userRepairApplyService;

    @Resource
    private UserAddressService userAddressService;

    @Resource
    private ClientUserService clientUserService;



    /**
     * 用户提交维修申请
     * @param common_failures
     * @param faultComment
     * @param faultCategory
     * @param userName
     * @param qrcode
     * @param userPhone
     * @param province
     * @param city
     * @param area
     * @param hasElevator
     * @param floorNumber
     * @param provinceName
     * @param cityName
     * @param areaName
     * @param addressContent
     * @return
     * @throws Exception
     */
    @RequestMapping("/p/saveUserRepairApply")
    @Transactional
    public Dto saveUserInformation(String common_failures, String faultComment, String faultCategory, String userName, String qrcode,
                                   String userPhone, String province, String city, String area, String hasElevator, String floorNumber,
                                   String provinceName, String cityName, String areaName, String addressContent)throws Exception{

        UserRepairApply apply = new UserRepairApply();
        apply.setId(IdWorker.getId());
        apply.setCommonFailures(common_failures);
        apply.setFaultComment(faultComment);
        apply.setFaultCategory(faultCategory);
        apply.setUserName(userName);
        apply.setUserPhone(userPhone);
        apply.setUserAddress(provinceName + cityName + areaName +" "+ addressContent);
        apply.setHasElevator(hasElevator);
        apply.setFloorNum(floorNumber);
        apply.setQrcode(qrcode);
        apply.setCreateTime(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));

        UserAddress address=new UserAddress();
        String addressId = IdWorker.getId();
        //企业信息，地址保存
        address.setId(addressId);
        //address.setUserId(userId);
        address.setUserName(userName);
        address.setUserPhone(userPhone);
        address.setAddressContent(addressContent);
        address.setProvince(province);
        address.setCity(city);
        address.setArea(area);
        address.setCreateTime(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));

        apply.setUserAddressId(addressId);

        int i = userRepairApplyService.addUserRepairApply(apply);
        i += userAddressService.itriptxAddUserAddress(address);
        if(i > 0){
            return DtoUtil.returnSuccess("信息提交成功");
        }
        return DtoUtil.returnFail("提交失败,请重试", "001");
    }


    /**
     * 分页获取用户维修申请
     * @param pageNumber
     * @param pageSize
     * @param userPhone
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getUserRepairApplyByPage")
    public Dto getUserRepairApplyByPage(Integer pageNumber, Integer pageSize, String userPhone) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("userPhone", userPhone);
        return DtoUtil.returnSuccess("获取成功", userRepairApplyService.queryUserRepairApplyPageByMap(param, pageNumber, pageSize));
    }


    /**
     * 电器厂根据申请单,直接下单
     * @param id
     * @param factoryId
     * @param is_protect
     * @param product_source
     * @param fixed_price
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/saveSubscribeOrder")
    public Dto saveSubscribeOrder(String id, String factoryId, String is_protect,String product_source, String fixed_price) throws Exception {
        UserRepairApply repairApply = userRepairApplyService.getUserRepairApplyById(id);
        ClientUser user = clientUserService.getClientUserById(factoryId);
        UserAddress userAddress = userAddressService.getUserAddressByOnceId(repairApply.getUserAddressId());
        AppOrder appOrder=new AppOrder();
        appOrder.setId(id);
        appOrder.setUserId(factoryId);
        appOrder.setCommonFailures(repairApply.getCommonFailures());
        appOrder.setProductSource(product_source);
        if(EmptyUtils.isNotEmpty(fixed_price)) {
            appOrder.setFixedPrice(Double.valueOf(fixed_price));
        }
        appOrder.setFloorNum(repairApply.getFloorNum());
        appOrder.setFaultType("3");
        appOrder.setFaultCategory(repairApply.getFaultCategory());
        appOrder.setHasElevator(repairApply.getHasElevator());
        appOrder.setIsprivateWork("0");
        appOrder.setFaultComment(repairApply.getFaultComment());
        appOrder.setCreateTime(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        appOrder.setOrderState("0");
        appOrder.setIs_again_order("0");
        appOrder.setUserAddress(repairApply.getUserAddress());
        appOrder.setUserName(repairApply.getUserName());
        appOrder.setUserPhone(repairApply.getUserPhone());
        appOrder.setUserType("2");
        appOrder.setUserAddressId(repairApply.getUserAddressId());
        appOrder.setRepairNumber(1);
        appOrder.setIs_protect(is_protect);
        appOrder.setCompanyName(user.getCompanyName());

        String area = userAddress.getArea();

        int  n = userRepairApplyService.userRepairApplyOrder(appOrder);

        // 发送极光推送给维修工
        String[] strSelect = new String[1];
        Map<String, String> cmap = new HashMap<String, String>();
        cmap.put("subscribeId", id);
        //区域内维修工
        List<Map<String, Object>> list =userRepairApplyService.getRepairUserByAreaIdOnlyWork(area);
        if(list.size() > 0){
            strSelect = new String[list.size()];
            for(int i=0;i<list.size();i++){
                strSelect[i] = list.get(i).get("id").toString();
            }
            Object obj = JPushUtil.testSendPush(strSelect, "您有新的预约订单，请查看", cmap);
            System.out.println(obj);
        }
        if(n>0){
            return DtoUtil.returnSuccess("提交成功","true");
        }else{
            return DtoUtil.returnFail("创建失败","002");
        }
    }



}
